package com.example.demo.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.demo.annotation.LogAop;
import com.example.demo.entity.TestEntity;
import com.example.demo.result.ResponseResult;
import com.example.demo.service.TestService;
import com.example.demo.utils.FdfsClientUtils;
import com.example.demo.vo.TestVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@Log4j2
@Controller
@RequestMapping("/test")
public class TestController {
    @Autowired
    private FdfsClientUtils fdfsClientWrapper;
    @Autowired
    private TestService testService;

    /***
     * 测试注解形式写sql
     * @return
     */
    @GetMapping("/testPlus")
    @ResponseBody
    public Object getAll() {
        return testService.getAll();
    }

    /***
     * 测试xml里写sql
     * @return
     */
    @GetMapping("/testMapper")
    @ResponseBody
    public Object getOne() {
        return ResponseResult.success(testService.getOne(1));
    }

    /***
     * 测试分页
     * @return
     */
    @GetMapping("/testPage")
    @ResponseBody
    public Object testPage() {
        Page<TestEntity> page = new Page<>(1, 1);
        TestVo testVo = new TestVo();
        testVo.setId(1);
        //上面参数前端传
        return ResponseResult.success(testService.selectByPage(page, testVo));
    }

    /***
     * 测试不写sql语句用代码操作sql
     * @return
     */
    @GetMapping("/testPage1")
    @ResponseBody
    public Object testPage1() {
        Page<TestEntity> page = new Page<>(1, 1);
        TestVo testVo = new TestVo();
//        testVo.setId(1);
        testVo.setName("t");
        //上面参数前端传
        return ResponseResult.success(testService.testPage1(page, testVo));
    }

    @GetMapping("/add")
    @ResponseBody
    public Object add() {
        TestEntity testEntity = new TestEntity();
        testEntity.setName("test");
        return ResponseResult.success(testService.add(testEntity));
    }

    @GetMapping("/update")
    @ResponseBody
    public ResponseResult<Object> update() {
        TestEntity testEntity = new TestEntity();
        testEntity.setName("修改的值");
        testEntity.setStatus("1");
        Object update = testService.update(testEntity);
        return ResponseResult.success(update);
    }

    @GetMapping("/a")
    @ResponseBody
    public Object a() {

        return ResponseResult.success(testService.a());
    }

    @GetMapping("/testAop")
    @ResponseBody
    @LogAop
    public Object testAop() {
        return ResponseResult.success();
    }


    /**
     * 上传文件
     *
     * @param file 文件
     * @return 文件路径
     */
    @RequestMapping(value = "upload")
    @ResponseBody
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        String filePath = null;
        try {
            filePath = fdfsClientWrapper.upload(file);
        } catch (Exception e) {
            log.error("上传文件异常：{}", e);
            return "上传文件失败";
        }
        return filePath;
    }

    /**
     * 下载文件
     *
     * @param filePath 文件路径
     * @return
     */
    @RequestMapping(value = "download")
    public void downloadFile(String filePath, HttpServletResponse response) {
        ServletOutputStream outputStream = null;
        try {
            byte[] bytes = fdfsClientWrapper.downloadFile(filePath);
            String fileName = "fdfs.jpg";
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.setCharacterEncoding("UTF-8");
            if (bytes != null) {
                outputStream = response.getOutputStream();
                outputStream.write(bytes);
                outputStream.flush();
            }
        } catch (IOException e) {
            log.debug("下载文件输出流异常：{}", e);
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                log.debug("下载文件关闭流异常：{}", e);
            }
        }
    }

    /**
     * 删除文件
     *
     * @param filePath 文件路径
     * @return 删除结果
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public String deleteFile(String filePath) {
        fdfsClientWrapper.deleteFile(filePath);
        return "删除成功";
    }

}
